<?php
/**
 * Blog整合模型
 *
 */
class Blog{
	/**
	 * 基本blog整合配置
	 *
	 * @var array
	 */
	var $config = array();

	/**
	 * MySQL数据库操作对象
	 *
	 * @var object
	 */
	var $mysql;

	var $sqladd;

	var $order;

	var $onlyimg;
	var $totalQuery;
	
	/**
	 * 构造函数
	 *
	 */
	function __construct()
	{
		global $very;
		if(!$very['aggreblog']) return ;
		$this->config['dbname'] = $very['blog_dbname'];
		$this->config['url'] = $very['blog_url'];
		$this->config['type'] = $very['blog_type'];
		$this->config['dbpre'] = $very['blog_dbpre'];
		$this->config['attachdir'] = $very['blog_attachdir'];
		$this->config['charset'] = $very['blog_charset'];
		if(eregi('^(http://)',$this->config['attachdir'])){
			$this->config['attachurl'] = $this->config['attachdir'].'/';
		}else{
			$this->config['attachurl'] = $this->config['url'].'/'.$this->config['attachdir'].'/';
		}
		$this->mysql = new DB($very['blog_dbhost'],$very['blog_dbuser'],$very['blog_dbpw'],$very['blog_dbname'],$very['blog_charset'],0); //另外一个DB类的实例
		//$this->setTableName();
	}

	function blog(){ //PHP4
		$this->__construct();
	}

	function readConfig($condition)
	{ //传入查询条件
		global $timestamp;
		$condition = unserialize($condition);
		foreach ($condition as $key=>$value){
			//if(!$this->getField($key)) continue;
			if($key=='fid'){
				if(empty($value)) continue;
				$value = explode(',',$value);
				$this->add($key,$value);
			}elseif ($key=='taxis'){
				if($value)
					$this->order='postdate';
				else
					$this->order='lastpost';
			}elseif ($key=='postdate' || $key=='lastpost'){
				if(empty($value)) continue;
				$value=$timestamp-$value*60*60*24;
				$this->add($key,$value);
			}else{
				if(empty($value)) continue;
				$this->add($key,$value);
			}
		}
	}

	/**
	 * 此方法来设置sql查询语句，根据一个字段以及相应的值来设置查询条件
	 *
	 * @param string $var
	 * @param mixed $value
	 */
	function add($var,$value)
	{
		$and = empty($this->sqladd) ? 'WHERE' : 'AND';
		//$field = $this->getField($var);
		if(is_array($value)){
			$value=implode(',',$value);
			$this->sqladd.=" $and i.$var IN ($value)";
		}else{
			$this->sqladd.=" $and i.$var>='$value'";
		}
	}
	
	function user($num,$order)
	{
		$userinfo = array();
		$num = intval($num);
		$order = addslashes($order);
		!$order && $order = 'blogs';
		$rs = $this->mysql->query("SELECT username,uid,$order AS value FROM {$this->config['dbpre']}user ORDER BY $order DESC LIMIT $num");
		while ($userdb = $this->mysql->fetch_array($rs)) {
			$userdb['title'] = strip_tags($userdb['username']);
			$userdb['url'] = $this->config['url']."/blog.php?uid=".$userdb['uid'];
			$userdb['value'] = $userdb[$order];
			$userinfo[] = $userdb;
		}
		return $userinfo;
	}
	
	function tags($num,$order)
	{
		$tagsinfo = array();
		$num = intval($num);
		$order = addslashes($order);
		!$order && $order = 'allnum';
		$rs = $this->mysql->query("SELECT tagid,tagname,$order AS value FROM {$this->config['dbpre']}tags ORDER BY $order DESC LIMIT $num");
		while ($tagsdb = $this->mysql->fetch_array($rs)) {
			$tagsdb['title'] = strip_tags($tagsdb['tagname']);
			$tagsdb['url'] = $this->config['url']."/tags.php?tagid=".$tagsdb['tagid'];
			$tagsdb['value'] = $tagsdb[$order];
			$tagsinfo[] = $tagsdb;
		}
		return $tagsinfo;	
	}
	
	function music($num,$order)
	{
		
	}
	
	function image($num,$order='')
	{
		$imagesinfo = array();
		$num = intval($num);
		$order = addslashes($order);
		//!$order && $order = 'allnum';
		$imageOrder = array('hits','replies','digest');
		if($order && in_array($order,$imageOrder)){
			$order = "ORDER BY i.$order DESC";
		}else{
			$order = "ORDER BY postdate DESC";
		}
		$rs = $this->mysql->query("SELECT * FROM {$this->config['dbpre']}items i LEFT JOIN  {$this->config['dbpre']}upload u USING(itemid) WHERE i.type='photo' $order LIMIT $num");
		while ($imagesdb = $this->mysql->fetch_array($rs)) {
			$imagesdb['title'] = strip_tags($imagesdb['subject']);
			$imagesdb['photo'] = $this->config['attachurl'].'/'.$imagesdb['attachurl'];
			$imagesdb['url'] = $this->config['url']."/blog.php?do=showone&uid=".$imagesdb['uid']."&type=photo&itemid=".$imagesdb['itemid'];
			$imagesdb['value'] = $imagesdb[$order];
			$imagesinfo[] = $imagesdb;
		}
		return $imagesinfo;		
	}

	/**
	 * 读取用户日志
	 *
	 * @param integer $start
	 * @param integer $num
	 * @return array
	 */
	function webLog($num)
	{
		$this->totalQuery = "SELECT COUNT(*) AS total FROM {$this->config['dbpre']}items i LEFT JOIN {$this->config['dbpre']}blog b USING(itemid)";
		$rs = $this->mysql->query("SELECT * FROM {$this->config['dbpre']}items i LEFT JOIN {$this->config['dbpre']}blog b USING(itemid) WHERE i.type='blog' ORDER BY i.postdate DESC  LIMIT $num");
		$bl = array();
		while ($bldb = $this->mysql->fetch_array($rs)) {
			$bldb['title'] = $bldb['subject'];
			$bldb['publisher']	= $bldb['author'];
			$bldb['ifpub']	= 1; //Blog调用全部为已经发布
			$bldb['url']	= $this->config['url']."/blog.php?do=showone&uid=$bldb[uid]&type=blog&itemid=$bldb[itemid]";
			$bl[]	= $bldb;
		}
		return $bl;
	}
	
	function total()
	{
		$total = $this->mysql->get_one($this->totalQuery);
		return $total['total'];	
	}

}
?>